<template>
  <a-modal
    :title="model && model?.id > 0 ? '编辑商品' : '新增商品'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="id">
          <a-input v-decorator="['id', { initialValue: model?.id }]" />
        </a-form-item>
        <a-form-item label="商品码">
          <a-input v-decorator="['code']" placeholder="请输入商品码"/>
        </a-form-item>
        <a-form-item label="商品名">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入商品名' }] }]" placeholder="请输入商品名"/>
        </a-form-item>
        <a-form-item label="品牌">
          <a-select v-decorator="['brandId', { rules: [{ required: true, message: '请输入品牌' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in brandOption" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="分类">
          <a-select v-decorator="['classId', { rules: [{ required: true, message: '请输入分类' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in classifyOption" :key="i.id">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="考核成本类型" v-show="!model?.id" >
          <a-select v-decorator="['assType']" placeholder="请选择">
            <a-select-option value="1">指定考核成本</a-select-option>
            <a-select-option value="2">浮动考核成本</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-show="!model?.id" label="考核成本">
          <a-input addon-after="元" v-decorator="['assCost']" placeholder="请输入考核成本"/>
        </a-form-item>
        <a-form-item v-show="!model?.id" label="零售价">
          <a-input addon-after="元" v-decorator="['price']" placeholder="请输入零售价" />
        </a-form-item>
        <a-form-item v-show="!model?.id" label="提成比例">
          <a-input addon-after="%" v-decorator="['propWage']" placeholder="请输入提成比例"/>
        </a-form-item>
        <a-form-item v-show="!model?.id" label="固定提成">
          <a-input addon-after="元" v-decorator="['fixWage']" placeholder="请输入固定提成"/>
        </a-form-item>
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model?.id > 0" label="状态">
          <a-select v-decorator="['status']" placeholder="请选择">
            <a-select-option value="1">在售</a-select-option>
            <a-select-option value="2">停售</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {
  getGdBrandListUsingPOST,
  getGdClassListUsingPOST,
} from '@/api/api/shangpinguanli.ts'
import pick from 'lodash.pick'

// 表单字段
const fields = ['code','name', 'brandId', 'classId', 'assCost', 'price', 'propWage', 'fixWage', 'status',  'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      form: this.$form.createForm(this),
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      brandOption: [],
      classifyOption: [],
    }
  },
  created() {
    this.getBrandOption()
    this.getClassifyOption()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    getBrandOption() {
      getGdBrandListUsingPOST(this.optionParam)
        .then((res) => {
          this.brandOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getClassifyOption() {
      getGdClassListUsingPOST({...this.optionParam,classType:2})
        .then((res) => {
          this.classifyOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
